<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;
use app\common\model\User;
use app\common\model\VipOrder;
use think\Db;
use think\Request;

/**
 * 首页接口
 */
class Vip extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * vip 配置
     * @return void 
     */
    public function vipList()
    {
        # 查询vip 配置
        $viplist  = Db::name('vip')->where('status', '1')->order('weigh', 'asc')->select();
        # 查询vip权益
        $vipbenefit = Db::name('vip_benefit')->where('status', '1')->order('weigh', 'asc')->select();
        $this->success("OK", [
            'viplist' => $viplist,
            'vipbenefit' => $vipbenefit,
        ]);
    }

    /**创建VIP订单 */
    public function create()
    {
        $vip_id = $this->request->param('vip_id');
        $user = $this->auth->getUser();
        # 查询vip 配置
        $vip = Db::name('vip')->where('id', $vip_id)->find();
        if (!$vip) {
            $this->error("vip 配置不存在");
        }
        # 创建vip订单
        $data = [
            'user_id' => $user['id'],
            'vip_id' => $vip_id,
            'money' => $vip['price'],
            'day' => $vip['day'],
            'simplified_batch' => $vip['simplified_batch'], #简批次数
            'createtime' => time(),
        ];
        $order_id = Db::name('vip_order')->insertGetId($data);
        if (!$order_id) {
            $this->error("创建订单失败");
        }
        $this->success("OK", ['order_id' => $order_id]);
    }

    /**支付VIP订单 */
    public function payOrder(Request $request)
    {
        $user_id = $this->auth->id;
        $param = $request->param();
        if (!in_array($param['pay_type'], ['wechat', 'alipay'])) {
            $this->error("支付参数错误");
        }
        $order_id = $param['order_id'];
        $order = VipOrder::get($order_id);
        if (!$order) {
            $this->error("订单不存在");
        }
        if (!$order->status != '1') $this->error("订单状态错误");
        $order->order_sn =  $order_sn = makePaySn($user_id, 'VIP');
        $order->pay_type = $param['pay_type'] == 'test' ? 'wechat' : $param['pay_type'];
        $order->save();
        # 测试支付
        if ($param['pay_type'] == 'test') {
            $pay = (new VipOrder())->notify_order($order_sn, 'test');
            $this->success("支付成功", compact('pay', 'order_id'));
        }
        # 正式支付
        if ($order_id) {
            $openid = '';
            if ($param['pay_type'] == 'wechat') {
                $user = new User();
                $openid = $user->where('id', $order->user_id)->value('openid');
                if (!$openid) {
                    $this->error("请先绑定微信");
                }
            }
            $params = [
                'amount' => $order->money,
                'orderid' => $order_sn,
                'type' => $param['pay_type'],
                'title' => "购买vip",
                'notifyurl' => $this->request->domain() . '/api/pay/notify/paytype/' . $param['pay_type'],
                'method' => 'miniapp',
                'openid' => $openid,
                'custom' => ['type' => 'set', 'id' => $order_id],
            ];

            $pay = Service::submitOrder($params);
        }
        if ($pay) {
            $this->success("支付成功", compact('pay', 'order_id'));
        } else {
            $this->error("支付失败");
        }
    }
}
